
package com.splic.common.core;

import com.alibaba.fastjson.JSONObject;

/**
 * 设备标识线程级 缓存
 */
public class DeviceContext {
    private static final ThreadLocal<JSONObject> THREADLOCAL_GROUPID_MAP = new ThreadLocal();

    public DeviceContext() {
    }


    public static String getDeviceIdentify() {
        if(THREADLOCAL_GROUPID_MAP.get() != null){
            return THREADLOCAL_GROUPID_MAP.get().getString("deviceIdentify");
        }
        return "";
    }

    public static String getVersion() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("version");
        }
        return "";
    }

    public static String getProductPackageKey() {
        if(THREADLOCAL_GROUPID_MAP.get() != null){
            return THREADLOCAL_GROUPID_MAP.get().getString("packageKey");
        }
        return "";
    }

    public static Long getDeviceId() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getLong("deviceId");
        }
        return null;
    }

    public static String getAppPackage() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("appPackage");
        }
        return null;
    }

    public static Long getChannelId() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getLong("channelId");
        }
        return null;
    }

    public static String getUserIdentify() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("userIdentify");
        }
        return null;
    }

    public static String getAndroidId() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("androidId");
        }
        return null;
    }

    public static String getImei() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("imei");
        }
        return null;
    }

    public static String getMac() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("mac");
        }
        return null;
    }

    public static String getOaid() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("oaid");
        }
        return null;
    }

    public static String getVaid() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("vaid");
        }
        return null;
    }

    public static String getGaid() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("gaid");
        }
        return null;
    }

    public static String getBaseOs() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("baseOS");
        }
        return null;
    }

    public static String getBranchOs() {
        if(THREADLOCAL_GROUPID_MAP.get() != null) {
            return THREADLOCAL_GROUPID_MAP.get().getString("branchOS");
        }
        return null;
    }


    public static void clearGlobalParams() {
        THREADLOCAL_GROUPID_MAP.remove();
    }


    public static void putGlobalParams(JSONObject globalParams) {
        THREADLOCAL_GROUPID_MAP.set(globalParams);
    }

   /* public static void putDeviceIdentify(String param, Map<String, String> groupMapping) {
        String deviceIdentify = getGroupIdByParam(param, groupMapping);
        if (StringUtils.isBlank(deviceIdentify)) {
            throw new IllegalArgumentException("Can not get GroupId,param:" + param);
        } else {
            putDeviceIdentify(deviceIdentify);
        }
    }

    public static String getGroupIdByParam(String param, Map<String, String> groupMapping) {
        if (MapUtils.isEmpty(groupMapping)) {
            throw new RuntimeException("GroupId Mapping is null !");
        } else {
            String result = null;
            Iterator var3 = groupMapping.entrySet().iterator();

            while(var3.hasNext()) {
                Entry<String, String> entry = (Entry)var3.next();
                if (StringUtils.equals(param, (CharSequence)entry.getValue())) {
                    result = (String)entry.getKey();
                    break;
                }
            }

            return result;
        }
    }*/
}
